#include <SDL/SDL_gfxPrimitives.h>

#include "gui_button.h"
#include "type_gui.h"

button* gui_button_new(main_obj *mobj, gchar *text) {
    button *b = g_malloc0(sizeof (button));
    b->ge.type = GUI_BUTTON;
    b->ge.redraw = TRUE;
    b->ge.posy = 0;
    b->ge.posx = 0;
    b->text = g_string_new(text);
    b->text_render = gui_util_drawtext(mobj->gm->current_font, 255, 255, 255, 255, 0, 0, 0, 0, b->text->str);
    b->ge.graphic = gui_util_get_new_empty_surface(b->text_render->w, b->text_render->h);
    b->click = gui_button_default_clicked;
    b->update = gui_button_update;
    b->paint = gui_button_normal_paint;
    return b;
}

gboolean gui_button_normal_paint(main_obj *mobj, button *b) {
    roundedBoxRGBA(b->ge.graphic, 0, 0, b->ge.graphic->w, b->ge.graphic->h, 5, 180, 180, 180, 205);
    SDL_BlitSurface(b->text_render, 0, b->ge.graphic, 0);
    return TRUE;
}

gboolean gui_button_direct_paint(main_obj *mobj, button *b, graphic_element *ge)
{
    //roundedBoxRGBA(ge->graphic, b->ge.posx, 0, )
}
gboolean gui_button_update(main_obj *mobj, button *b) {
    if (mobj->event->type == SDL_MOUSEBUTTONDOWN) {
        if (gui_util_element_is_clicked(mobj, (graphic_element*) b)) {
            b->click(mobj, b);
        }
    }
    return TRUE;
}

gboolean gui_button_default_clicked(main_obj *mobj, button *b) {
    g_print("clicked button\n");
    return TRUE;
}

